import {WeaBrowser,WeaTools} from 'ecCom'

export default class Main extends React.Component{
    constructor(props) {
        super(props);
        this.state = {
            support: false,
            browserProp: {}
        }
        this.getBrowserProp(props);
    }
    componentWillReceiveProps(nextProps) {
        if(this.props.type !== nextProps.type){
            this.setState({support: false});    //做一次清空
            this.getBrowserProp(nextProps);
        }
    }
    shouldComponentUpdate(nextProps,nextState){
        return this.props.type !== nextProps.type
            || this.props.value !== nextProps.value
            || this.props.filterParam !== nextProps.filterParam
            || this.state.support !== nextState.support
            || this.state.browserProp !== nextState.browserProp
    }
    getBrowserProp(props){
        const {type,filterParam} = props;
        const params = {browsertype: type};
        WeaTools.callApi("/api/workflow/wfutil/getBrowserProp", "GET", params).then(datas =>{
            let browserProp = datas.browserProp;
            if(!!filterParam)
                browserProp.dataParams = filterParam;
            this.setState({
                support: datas.support,
                browserProp: browserProp
            });
        });
    }
    doChangeEvent(ids,names,datas){
        this.props.onChange(ids,names,datas,this.state.browserProp);
    }
    render(){
        const {type,name,value,resize,style,viewAttr,onChange,filterParam} = this.props;
        let {support,browserProp} = this.state;
        delete browserProp.replaceDatas;
        let baseProps = {value:value, fieldName:name, resize:resize, style:style, viewAttr:viewAttr};
        if(support){
            return (
                <WeaBrowser {...{...browserProp, ...baseProps,}}
                    selectSize='default'
                    onChange={this.doChangeEvent.bind(this)}
                />
            )
        }else{
            return <span></span>
        }
    }
}